package com.hxzy.controller;

import com.hxzy.dao.ClassRoomMapper;
import com.hxzy.dao.ClazzMapper;
import com.hxzy.dao.CourseMapper;
import com.hxzy.dao.TimeMapper;
import com.hxzy.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("coures")
public class CouresController {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private ClassRoomMapper classRoomMapper;
    @Autowired
    private TimeMapper timeMapper;

    @RequestMapping(value = "all", method = RequestMethod.GET)
    @ResponseBody
    public Result getAllCoures(){
    List<Course> courses = courseMapper.selectAll();
    return new Result().ok().addData(courses);
}

    //添加课程
    @RequestMapping("addcoures")
    @ResponseBody
    public Result addcoures(String title) {
        if (title == null || "".equals(title))
            return new Result().error("参数不能为空");
        Course course = new Course();
        course.setCoursename(title);
        int insert = courseMapper.insert(course);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("添加失败");
    }    //添加课程

    @RequestMapping("deletebycourId/{cid}")
    @ResponseBody
    public Result deletecoures(@PathVariable("cid") String cid) {
        int i = courseMapper.deleteByPrimaryKey(cid);
        if (i > 0)

            return new Result().ok();
        else
            return new Result().error("添加失败");
    }

    //    修改课程
    @RequestMapping("updatecoures")
    @ResponseBody
    public Result updateCoures(String title, Integer id) {
        Course course = new Course();
        course.setCoursename(title);
        course.setId(id);
        int i = courseMapper.updateByPrimaryKeySelective(course);
        if (i > 0) {
            return new Result().ok();
        } else
            return new Result().error("修改失败");
    }


//    班级管理

    //    添加班级
    @RequestMapping("addclass")
    @ResponseBody
    public Result addClazz(String title) {
        if (title == null || "".equals(title))
            return new Result().error("参数不能为空");
        Clazz clazz = new Clazz();
        clazz.setClazzname(title);
        int insert = clazzMapper.insert(clazz);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");

    }//    添加班级

    @RequestMapping("deleteclass/{id}")
    @ResponseBody
    public Result deleteClazz(@PathVariable("id") Integer  clazz) {
        int insert = clazzMapper.deleteByPrimaryKey(clazz);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");

    }

    @RequestMapping(value = "updateclass",method = RequestMethod.POST)
    @ResponseBody
    public Result updateClass(String title, Integer id) {
        Clazz clazz = new Clazz();
        clazz.setClazzname(title);
        clazz.setId(id);
        int insert = clazzMapper.updateByPrimaryKeySelective(clazz);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");
    }


    //    教师管理

    //    添加教师
    @RequestMapping("addclassroom")
    @ResponseBody
    public Result addClazzRoom(Integer title) {
        if (title == null)
            return new Result().error("参数不能为空");
        ClassRoom clazz = new ClassRoom();
        clazz.setClassroom(title);

        int insert = classRoomMapper.insert(clazz);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");

    }//    添加班级

    @RequestMapping("deleteclassroom/{id}")
    @ResponseBody
    public Result deleteClazzRoom(@PathVariable("id") String clazz) {
        int insert = classRoomMapper.deleteByPrimaryKey(clazz);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");

    }

    @RequestMapping("updateclassroom")
    @ResponseBody
    public Result updateClassRoom(Integer title, Integer id) {
        ClassRoom clazz = new ClassRoom();
        clazz.setClassroom(title);
        clazz.setId(id);
        int insert = classRoomMapper.updateByPrimaryKeySelective(clazz);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");
    }


//    时间管理


    @RequestMapping("addtime")
    @ResponseBody
    public Result addtime(String title) throws ParseException {
        if (title == null)
            return new Result().error("参数不能为空");
        Time time = new Time();
        time.setTime(title);
        int insert = timeMapper.insert(time);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");

    }//    添加班级

    @RequestMapping("deletetime/{id}")
    @ResponseBody
    public Result deletetime(@PathVariable("id") String clazz) {
        int insert = timeMapper.deleteByPrimaryKey(clazz);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");
    }

    @RequestMapping("updatetime")
    @ResponseBody
    public Result updatetime(String title, Integer id) {
        Time time = new Time();
        time.setTime(title);
        time.setId(id);
        int insert = timeMapper.updateByPrimaryKeySelective(time);
        if (insert > 0)

            return new Result().ok();
        else
            return new Result().error("失败");
    }


    //    查询所有
    @RequestMapping("selectallbycourse")
    @ResponseBody
    public Result seletcAllBycourse() {
        List<Course> courses = courseMapper.selectAll();
        return new Result().ok().addData(courses);
    }

    @RequestMapping("selectallbyclass")
    @ResponseBody
    public Result seletcAllByclass() {
        List<Clazz> clazzes = clazzMapper.selectAll();
        return new Result().ok().addData(clazzes);
    }

    @RequestMapping("selectallbyclassroom")
    @ResponseBody
    public Result seletcAllByclassroom() {
        List<ClassRoom> classRooms = classRoomMapper.selectAll();
        return new Result().ok().addData(classRooms);
    }

    @RequestMapping("selectallbytime")
    @ResponseBody
    public Result seletcAllBytime() {
        List<Time> times = timeMapper.selectAll();
        return new Result().ok().addData(times);
    }




}
